package com.freemark.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
//网页静态化处理
@Controller
public class freemarkController {
	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	
	@RequestMapping("/getHtml")
	@ResponseBody
	public String getHtml() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException{
		//生成静态页面
		Configuration configuration = freeMarkerConfigurer.getConfiguration();
		Template template = configuration.getTemplate("hello.ftl");
		Map map = new HashMap<>();
		map.put("hello", "spring freemark test");
		//7:创建输出流对象,指明静态页面的路径和名称
		Writer out=new FileWriter(new File("f:\\Freemark-shfreemarker\\test.html"));
		//8:生成静态页面
		template.process(map, out);
		out.close();
		//返回结果
		return "OK";
	}
	
}
